import React, { Component } from 'react'

export default class App extends Component {
  state={
    isShow:true
  }
  render() {
    return (
      <div>
        <button onClick={()=>{
          this.setState({
            isShow:!this.state.isShow
          })
        }}>点击</button>
        {/* 切换组件显示(销毁) */}
        {/* {this.state.isShow ?<Child/> :""} */}
        {this.state.isShow &&<Child/> }
      </div>
    )
  }
 
}

class Child extends Component{
  componentDidMount() { 
    window.onresize=()=>{
      console.log("resize");
    }
    this.timer=setInterval(()=>{
      console.log("定时器");
    },1000)
   }
  // 在删除组件之前进行清理操作，比如计算器和事件监听
  componentWillUnmount() {
    console.log("componentWillUnmount");
    window.onresize=null
    clearInterval(this.timer)
   }

  render() {
    return (
      <div>Child</div>
    )
  }
}